<?php

if(! defined('OT_ROOT')) {
	exit('Access Denied');
}


class Nav{

	// 传统导航网址?m=1&p=1（除了文章、淘客、论坛等导航[TplList::NewsListNav]外，其他功能列表导航用这个）
	public static function Show($URL, $pageCount, $pageSize, $recordCount, $skin='img', $pageMode='pageNum', $mode=''){
		$retStr = '';

		if ($mode == 'uc'){
			$pageStyle = '';
			$selectStyle = 'padding:6px;';
		}else{
			$pageStyle = 'margin-top:5px;';
			$selectStyle = 'padding:0px;';
		}

		// 页码风格
		switch($skin){
			case 'CN':
				$first_page	='[第一页]';
				$prev_page	='[上一页]';
				$next_page	='[下一页]';
				$last_page	='[最后页]';
				$first_page2='<span class="fontNav_2d">[第一页]</span>';
				$prev_page2	='<span class="fontNav_2d">[上一页]</span>';
				$next_page2	='<span class="fontNav_2d">[下一页]</span>';
				$last_page2	='<span class="fontNav_2d">[最后页]</span>';
				break;
			default:
	//		case 'img':
				$first_page	= '<img src="inc_img/navigation/narStart.gif" border="0" alt="第一页" style="'. $pageStyle .'" class="navBtnD" />';
				$prev_page	= '<img src="inc_img/navigation/narLast.gif" border="0" alt="上一页" style="'. $pageStyle .'" />';
				$next_page	= '<img src="inc_img/navigation/narNext.gif" border="0" alt="下一页" style="'. $pageStyle .'" />';
				$last_page	= '<img src="inc_img/navigation/narEnd.gif" border="0" alt="最后页" style="'. $pageStyle .'" />';
				$first_page2= '<img src="inc_img/navigation/narStart2.gif" border="0" alt="第一页" style="'. $pageStyle .'" class="navBtnD" />';
				$prev_page2	= '<img src="inc_img/navigation/narLast2.gif" border="0" alt="上一页" style="'. $pageStyle .'" />';
				$next_page2	= '<img src="inc_img/navigation/narNext2.gif" border="0" alt="下一页" style="'. $pageStyle .'" />';
				$last_page2	= '<img src="inc_img/navigation/narEnd2.gif" border="0" alt="最后页" style="'. $pageStyle .'" />';
				break;
	/*
			default:
				$first_page	='<span style="font-family:webdings;">9</span>';
				$prev_page	='<span style="font-family:webdings;">7</span>';
				$next_page	='<span style="font-family:webdings;">8</span>';
				$last_page	='<span style="font-family:webdings;">:</span>';
				$first_page2='<span style="font-family:webdings;" class="fontNav_2d">9</span>';
				$prev_page2	='<span style="font-family:webdings;" class="fontNav_2d">7</span>';
				$next_page2	='<span style="font-family:webdings;" class="fontNav_2d">8</span>';
				$last_page2	='<span style="font-family:webdings;" class="fontNav_2d">:</span>';
				break;
	*/
		}

		// 往URL里填补GET参数
		$URL = OT::GetParam(array('page'));
		$parSign = OT::ParamSign($URL);

		$page = intval(@$_GET['page']);

		if ($page < 1 || $page > $pageCount){ $page = 1; }
			if (in_array($pageMode, array('pageNum','users'))){
				$retStr .= '<div id="navShowInfo" class="navBtn fontNav_2">第'. $page .'/'. $pageCount .'页&ensp;&ensp;共'. $recordCount .'条记录</div>';
			}

		if ($page<=1){
			$retStr .= '
			<span class="navBtn">'. $first_page2 .'</span>
			<span class="navBtn">'. $prev_page2 .'</span>
			';
		}else{
			$retStr .= '
			<a href="'. $URL .'" class="navBtnPointer fontNav_2">'. $first_page .'</a>
			<a href="'. $URL . $parSign .'page='. ($page - 1) .'" class="navBtnPointer fontNav_2">'. $prev_page .'</a>
			';
		}

		$showPageNum=8;	//显示页码个数
		$pageNumHalf=intval($showPageNum/2);

		if ($pageCount <= $showPageNum){
			$startpage = 1;
			$endpage = $pageCount;
		}elseif (($page - $pageNumHalf) >= 1 && ($page + $pageNumHalf) <= $pageCount){
			$startpage = $page-$pageNumHalf;
			$endpage = $page+$pageNumHalf;
		}elseif (($page - $pageNumHalf) < 1){
			$startpage = 1;
			$endpage = $showPageNum;
		}elseif (($page + $pageNumHalf) > $pageCount){
			$startpage = $pageCount-($showPageNum-1);
			$endpage = $pageCount;
		}
		
		for ($i=$startpage; $i<=$endpage; $i++){
			if ($i == $page){
				$retStr .= '<span class="navBtn fontNav2_2">'. $i .'</span>';
			}else{
				$retStr .= '<a href="'. $URL . ($i<=1 ? '' : $parSign .'page='. $i) .'" class="navBtnPointer fontNav_2">'. $i .'</a>';
			}
		}

		if ($page >= $pageCount){
			$retStr .= '
			<span class="navBtn">'. $next_page2 .'</span>
			<span class="navBtn">'. $last_page2 .'</span>
			';
		}else{
			$retStr .= '
			<a href="'. $URL . $parSign .'page='. ($page + 1) .'" class="navBtnPointer fontNav_2">'. $next_page .'</a>
			<a href="'. $URL . $parSign .'page='. $pageCount .'" class="navBtnPointer fontNav_2">'. $last_page .'</a>
			';
		}

		$retStr .= '
		<div id="navShowGoPage" class="navBtn">
		<div>
			<select onchange="if(this.value!=\'\'){if (this.value==\'1\'){document.location.href=\''. $URL .'\';}else{document.location.href=\''. $URL . $parSign .'page=\'+ this.value}}" style="padding:0px;">
				<option value=""></option>
				';
				for ($i=1; $i<=$pageCount; $i++){
					$retStr .= '<option value="'. $i .'">'. $i .'</option>';
				}
			$retStr .= '
			</select>
		</div>
		</div>';

		global $tpl,$userRow;
		if ( isset($tpl) && isset($userRow) && in_array($tpl->webTypeName, array('usersCenter','agentCenter')) ){
			if ($tpl->webTypeName == 'agentCenter'){
				$pageNum = $userRow['AU_pageNum'];
			}else{
				$pageNum = $userRow['UE_pageNum'];
			}
			$optionStr = '';
			for ($i=10; $i<=100; $i+=10){
				$optionStr .= '<option value="'. $i .'">'. $i .'</option>';	//  '. Is::Selected($pageNum,$i) .'
			}
			// <span class="fontNav_2 navBtn">每页<select onchange="RevUserPageNum(this.value)"><option value="'. $pageNum .'">'. $pageNum .'</option>'. $optionStr .'</select>条</span>
			$retStr .= '
			<span class="fontNav_2">&ensp;&ensp;每页<select onchange="RevUserPageNum(this.value)" style="padding:5px;"><option value="'. $pageNum .'">'. $pageNum .'</option>'. $optionStr .'</select>条</span>
			';
		}

		if ($mode == 'get' || $mode == 'uc'){
			return $retStr;
		}else{
			echo($retStr);
		}
	}



	// 导航
	public static function Ajax($outputID,$pageCount,$pageSize,$recordCount,$skin=''){
		$retStr = '';

		//页码风格
		switch($skin){
			case 'CN':
				$first_page	='[第一页]';
				$prev_page	='[上一页]';
				$next_page	='[下一页]';
				$last_page	='[最后页]';
				$first_page2='<span class="fontNav_2d">[第一页]</span>';
				$prev_page2	='<span class="fontNav_2d">[上一页]</span>';
				$next_page2	='<span class="fontNav_2d">[下一页]</span>';
				$last_page2	='<span class="fontNav_2d">[最后页]</span>';
				break;
			case 'img':
				$first_page	= '<img src="inc_img/navigation/narStart.gif" border="0" alt="第一页" style="margin-top:5px;" class="navBtnD" />';
				$prev_page	= '<img src="inc_img/navigation/narLast.gif" border="0" alt="上一页" style="margin-top:5px;" />';
				$next_page	= '<img src="inc_img/navigation/narNext.gif" border="0" alt="下一页" style="margin-top:5px;" />';
				$last_page	= '<img src="inc_img/navigation/narEnd.gif" border="0" alt="最后页" style="margin-top:5px;" />';
				$first_page2= '<img src="inc_img/navigation/narStart2.gif" border="0" alt="第一页" style="margin-top:5px;" class="navBtnD" />';
				$prev_page2	= '<img src="inc_img/navigation/narLast2.gif" border="0" alt="上一页" style="margin-top:5px;" />';
				$next_page2	= '<img src="inc_img/navigation/narNext2.gif" border="0" alt="下一页" style="margin-top:5px;" />';
				$last_page2	= '<img src="inc_img/navigation/narEnd2.gif" border="0" alt="最后页" style="margin-top:5px;" />';
				break;
			default:
				$first_page	='<span style="font-family:webdings;">9</span>';
				$prev_page	='<span style="font-family:webdings;">7</span>';
				$next_page	='<span style="font-family:webdings;">8</span>';
				$last_page	='<span style="font-family:webdings;">:</span>';
				$first_page2='<span style="font-family:webdings;" class="fontNav_2d">9</span>';
				$prev_page2	='<span style="font-family:webdings;" class="fontNav_2d">7</span>';
				$next_page2	='<span style="font-family:webdings;" class="fontNav_2d">8</span>';
				$last_page2	='<span style="font-family:webdings;" class="fontNav_2d">:</span>';
				break;
		}

		// 往URL里填补GET参数
		$URL = basename($_SERVER['PHP_SELF']) . OT::GetParam(array('page'));

		$page = intval(@$_GET['page']);


		if ($page<1 || $page>$pageCount){$page=1;}
			$retStr .= '<div id="navAjaxInfo" class="navBtn fontNav_2">第'. $page .'/'. $pageCount .'页&ensp;&ensp;共'. $recordCount .'条记录</div>';

		if ($page<=1){
			$retStr .= '
			<span class="navBtn">'. $first_page2 .'</span>
			<span class="navBtn">'. $prev_page2 .'</span>
			';
		}else{
			$retStr .= '
			<a href="#" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\',1);return false;" class="navBtnPointer fontNav_2">'. $first_page .'</a>
			<a href="#" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\','. ($page - 1) .');return false;" class="navBtnPointer fontNav_2">'. $prev_page .'</a>
			';
		}

		$showPageNum=8;	//显示页码个数
		$pageNumHalf=intval($showPageNum/2);

		if ($pageCount <= $showPageNum){
			$startpage = 1;
			$endpage = $pageCount;
		}elseif (($page-$pageNumHalf) >= 1 && ($page+$pageNumHalf) <= $pageCount){
			$startpage = $page-$pageNumHalf;
			$endpage = $page+$pageNumHalf;
		}elseif (($page-$pageNumHalf) < 1){
			$startpage = 1;
			$endpage = $showPageNum;
		}elseif (($page+$pageNumHalf) > $pageCount){
			$startpage = $pageCount-($showPageNum-1);
			$endpage = $pageCount;
		}
		
		for ($i=$startpage; $i<=$endpage; $i++){
			if ($i == $page){
				$retStr .= '<span id="clickNowPage" class="navBtn fontNav2_2" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\','. $page .')">'. $i .'</span>';
			}else{
				$retStr .= '<a class="navBtnPointer fontNav_2" href="#" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\','. $i .');return false;">'. $i .'</a>';
			}
		}

		if ($page >= $pageCount){
			$retStr .= '
			<span class="navBtn">'. $next_page2 .'</span>
			<span class="navBtn">'. $last_page2 .'</span>
			';
		}else{
			$retStr .= '
			<a href="#" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\','. ($page + 1) .');return false;" class="navBtnPointer fontNav_2">'. $next_page .'</a>
			<a href="#" onclick="AjaxNavHref(\''. $outputID .'\',\''. $URL .'\','. $pageCount .');return false;" class="navBtnPointer fontNav_2">'. $last_page .'</a>
			';
		}

		$retStr .= '
		<div id="navAjaxGoPage" class="navBtn" style="padding-top:1px;height:22px;">
		<div>
			<select onchange="if(this.value!=\'\'){AjaxNavHref(\''. $outputID .'\',\''. $URL .'\',this.value);}" style="padding:0px;">
				<option value="">&ensp;</option>
				';
				for ($i=1; $i<=$pageCount; $i++){
					$retStr .= '<option value="'. $i .'">'. $i .'</option>';
				}
			$retStr .= '
			</select>
		</div>
		</div>
		';

		return $retStr;
	}



	// 手机版列表导航
	public static function WapShow($typeStr,$typeDir,$typeID,$page,$pageCount,$pageSize,$recordCount, $maxPage=0, $event='',$judGet=true){
		global $DB,$tpl;

		$retStr = '';

		if ($typeStr == 'refer'){
			$typeStr .= '-'. $tpl->refType .'-'. OT::UrlEn($tpl->refContent);
		}elseif (strpos('|mark|user|topic|','|'. $typeStr .'|') !== false){
			$typeStr .= '-'. OT::UrlEn($tpl->markName) .'';
		}

		$retStr .= '<div class="navPageBox bodyActive"><ul>';

		if ($page<=1){
			$retStr .= '<li style="border-bottom:none;"><span class="span">上一页</span></li>';
		}else{
			$retStr .= '<li style="border-bottom:none;"><a href="'. self::NavHref($typeStr,$typeDir,$typeID,$page - 1,$maxPage) .'" title="上一页">上一页</a></li>';
		}

		if ($maxPage > 0 && strpos($event,'|listMaxPageEnd|') !== false && $pageCount > $maxPage){
			$pageCount = $maxPage;
			$recordCount = $maxPage * $pageSize;
		}

		$retStr .= '
		<li class="gotoLeft" style="width:40%; border-bottom:none;"><input type="text" id="navPageVal" name="navPageVal"  placeholder="'. $page .'/'. $pageCount .'" style="background-color:#fff;"></li>
		<li class="gotoRight" style="border-bottom:none;"><a href="javascript:void(0);" onclick=\'if(document.getElementById("navPageVal").value!=""){ListPageHref2(document.getElementById("navPageVal").value,'. $maxPage .',"'. self::NavHref_pageSign($typeStr,$typeDir,$typeID,2,$maxPage) .'","'. self::NavHref_pageSign($typeStr,$typeDir,$typeID,2,1) .'");}\' >跳页</a></li>
		';


		if ($page >= $pageCount){
			$retStr .= '<li style="border-bottom:none;"><span class="span">下一页</span></li>';
		}else{
			$retStr .= '<li style="border-bottom:none;"><a href="'. self::NavHref($typeStr,$typeDir,$typeID,$page + 1,$maxPage) .'" title="下一页">下一页</a></li>';
		}

		$retStr .= '</ul><div class="clr"></div></div>';

		if ($judGet){
			return $retStr;
		}else{
			echo($retStr);
		}
	}

	public static function NavHref($typeStr,$typeDir,$typeID,$page,$maxPage=0){
		global $tpl;

		if ($typeStr == 'page'){
			return OT::GetParam(array('page'), $page > 1 ? array('page'=>$page) : array());
		}elseif ($typeStr == 'tkGoods'){
			return Url::GoodsList($typeID, $page, $tpl->webPathPart);
		}elseif ($typeDir == 'tkGoods'){
			return Url::GoodsListStr($typeStr, $page, $tpl->webPathPart);
		}elseif ($typeDir == 'bbs'){
			if ($typeStr == 'bbsShow'){
				return Url::BbsID($typeID, $page, $tpl->webPathPart);
			}elseif ($typeStr != '' && $typeID == 0){
				return Url::BbsListStr($typeStr, $page, $tpl->webPathPart);
			}else{
				return Url::BbsList($typeID, $page, $tpl->webPathPart);
			}
		}else{
			if ($typeStr!='' && $typeID==0){
				return Url::ListStr($typeStr, $page, $tpl->webPathPart);
			}else{
				return Url::ListID('', $typeDir, $typeID, $page, $tpl->webPathPart, $maxPage);
			}
		}
	}

	public static function NavHref_pageSign($typeStr,$typeDir,$typeID,$page,$maxPage=0){
		global $tpl;

		if ($typeStr == 'page'){
			return OT::GetParam(array('page'),$page > 1 ? array('page'=>'[page]') : array());
		}elseif ($typeStr == 'tkGoods'){
			return Url::GoodsList_pageSign($typeID, $page, $tpl->webPathPart);
		}elseif ($typeDir == 'tkGoods'){
			return Url::GoodsListStr_pageSign($typeStr,$page, $tpl->webPathPart);
		}elseif ($typeDir == 'bbs'){
			if ($typeStr == 'bbsShow'){
				return Url::BbsID_pageSign($typeID, $page, $tpl->webPathPart);
			}elseif ($typeStr != '' && $typeID == 0){
				return Url::BbsListStr_pageSign($typeStr, $page, $tpl->webPathPart);
			}else{
				return Url::BbsList_pageSign($typeID, $page, $tpl->webPathPart);
			}
		}else{
			if ($typeStr!='' && $typeID==0){
				return Url::ListStr_pageSign($typeStr, $page, $tpl->webPathPart);
			}else{
				return Url::ListID_pageSign('',$typeDir, $typeID, $page, $tpl->webPathPart,$maxPage);
			}
		}
	}

}

?>